$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'manage/brand/list',
        datatype: "json",
        colModel: [
            {label: '品牌ID', name: 'id', index: "id", width: 45, key: true},
            {label: '品牌名称', name: 'name', width: 75},
            {label: '品牌首字母', name: 'firstChar', width: 90},
            {
                label: '操作', name: '', width: 40, formatter: function (cellvalue, options, row) {
                    return '<span class="label label-default" onclick="vm.update(\'' + row.id + '\')" style="cursor:pointer;">修改</span>';
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        brand: {
            id: 1,
            name: null,
            firstChar: null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            console.log("add");
            vm.showList = false;
            vm.title = "新增";
            vm.brand = {};
        },
        update: function (id) {
            console.log(id)
        },
        saveOrUpdate: function () {
            var url = vm.brand.id == null ? "sys/user/save" : "sys/user/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.brand),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {},
                page: page
            }).trigger("reloadGrid");
        }
    }
});